﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DataBase.Core;

namespace MeetFuture.MRC
{
    public class ApplicationDomain
    {        
        private static ApplicationDomain m_instance;
        private static Object m_lock = typeof(ApplicationDomain);
        private static DataBaseHelper_ERack dbHelper_ERack;
        private static DataBaseHelper_MRC dbHelper_MRC;

        public static ApplicationDomain Instance
        {
            get
            {
                lock (m_lock)
                {
                    if (m_instance == null)
                    {
                        lock (m_lock)
                        {
                            m_instance = new ApplicationDomain();
                        }
                    }
                }                
                return m_instance;
            }
        }

        private ApplicationDomain()
        {
            try
            {
                ApplicationGlobal.ConnectString_ERack = GetAppConfig(ApplicationGlobal.key_erackdb);                
                dbHelper_ERack = new DataBaseHelper_ERack();
                ApplicationGlobal.ConnectString_MRC = GetAppConfig(ApplicationGlobal.key_mrcdb);
                dbHelper_MRC = new DataBaseHelper_MRC();
            }
            catch (System.Exception ex)
            {
            }
        }

        public DataBaseHelper_ERack DBHelper_ERack
        {
            get { return dbHelper_ERack; }
        }

        public DataBaseHelper_MRC DBHelper_MRC
        {
            get { return dbHelper_MRC; }
        }

        public static void Reset()
        {
            lock (m_lock)
            {
                m_instance = null;
            }
        }

        public string GetERSSubject()
        {
            string result = string.Empty;
            try
            {               
                string tmp = GetAppConfig(ApplicationGlobal.key_rv_ers).Trim('.') + ".>";
                result = tmp;
            }
            catch (System.Exception ex)
            {
            }
            return result;
        }

        public string GetMyRVSubject()
        {
            string result = string.Empty;
            try
            {
                string tmp = GetAppConfig(ApplicationGlobal.key_this).Trim('.') + "." + ApplicationGlobal.key_mrcsrv + ".>";
                result = tmp;
            }
            catch (System.Exception ex)
            {
            }
            return result;
        }

        public string GetEAPServerSubject()
        {
            string result = string.Empty;
            try
            {
                string tmp = GetAppConfig(ApplicationGlobal.key_this).Trim('.') + "." + ApplicationGlobal.key_eapsrv + ".>";
                result = tmp;
            }
            catch (System.Exception ex)
            {
            }
            return result;
        }

        public string GetEAPSubject(string Tool)
        {
            string result = string.Empty;
            try
            {
                string tmp = GetAppConfig(ApplicationGlobal.key_rv_eap).Trim('.') + "." + Tool + ".>";
                result = tmp;
            }
            catch (System.Exception ex)
            {
            }
            return result;
        }

        public string GetMRDServerSubject()
        {
            string result = string.Empty;
            try
            {
                string tmp = GetAppConfig(ApplicationGlobal.key_this).Trim('.') + "." + ApplicationGlobal.key_mrdsrv + ".>";
                result = tmp;
            }
            catch (System.Exception ex)
            {
            }
            return result;
        }

        public string GetFMBServerSubject()
        {
            string result = string.Empty;
            try
            {
                string tmp = GetAppConfig(ApplicationGlobal.key_this).Trim('.') + "." + ApplicationGlobal.key_fmbsrv + ".>";
                result = tmp;
            }
            catch (System.Exception ex)
            {
            }
            return result;
        }

        public string GetQueryServerSubject()
        {
            string result = string.Empty;
            try
            {
                string tmp = GetAppConfig(ApplicationGlobal.key_this).Trim('.') + "." + ApplicationGlobal.key_querysrv + ".>";
                result = tmp;
            }
            catch (System.Exception ex)
            {
            }
            return result;
        }

        public Dictionary<string, string> GetConfigValues(string key)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            try
            {
                string[] values = GetAppConfig(key).Split(ApplicationGlobal.configSplitChar, StringSplitOptions.RemoveEmptyEntries);
                foreach (string value in values)
                {
                    result.Add(value.Trim(), value.Trim());
                }
            }
            catch (System.Exception ex)
            {
            }
            return result;
        }

        /// <summary>
        /// 以XML文件方式保存Config文件
        /// </summary>
        /// <param name="appKey"></param>
        /// <param name="appValue"></param>
        public void SetAppConfig(string appKey, string appValue)
        {
            try
            {
                System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
                xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
                System.Xml.XmlNode xNode = xDoc.SelectSingleNode("//appSettings");
                System.Xml.XmlElement xElem = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
                if (xElem != null) xElem.SetAttribute("value", appValue);
                else
                {
                    System.Xml.XmlElement xNewElem = xDoc.CreateElement("add");
                    xNewElem.SetAttribute("key", appKey);
                    xNewElem.SetAttribute("value", appValue);
                    xNode.AppendChild(xNewElem);
                }
                xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
            }
            catch (System.Exception ex)
            {
            }
        }

        /// <summary>
        /// 以XML文件方式读取Config文件
        /// </summary>
        /// <param name="appKey"></param>
        /// <param name="appValue"></param>
        public string GetAppConfig(string appKey)
        {
            try
            {
                System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
                xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
                System.Xml.XmlNode xNode = xDoc.SelectSingleNode("//appSettings");
                System.Xml.XmlElement xElem = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
                if (xElem != null)
                {
                    return xElem.Attributes["value"].Value;
                }
            }
            catch (System.Exception ex)
            {
            }
            return string.Empty;
        }

        public string getHostIP()
        {
            string IP = "";
            string HostName = System.Net.Dns.GetHostName();
            System.Net.IPHostEntry MyEntry = System.Net.Dns.GetHostEntry(HostName);
            System.Net.IPAddress[] MyAddress = MyEntry.AddressList;
            for (int i = 0; i < MyAddress.Length; i++)
            {
                try
                {
                    if (MyAddress[i].ToString().IndexOf(":") < 0)
                    {
                        if (MyAddress[i].ToString().IndexOf(".") >= 0)
                        {
                            IP = MyAddress[i].ToString();
                            break;
                        }
                    }
                }
                catch
                {
                    IP = "None";
                }
            }
            return IP;
        }

    }
}
